import threading
import time

from redis import Redis


def kv():
    redis_client.set('a', 1)  # 每一个set操作返回 0失败 1成功
    redis_client.setex('b', 10, '2')
    print(redis_client.exists('a'))
    print(redis_client.exists('b'))
    print(redis_client.exists('c'))
    res = redis_client.exists('a', 'c', 'b')
    print(res)


def hash_op():
    print(redis_client.exists('hash'))
    print(redis_client.hexists('hash', 'name'))
    redis_client.hmset('hash', {'name': 'hy', 'id': 1, 'ls': ','.join(['1', '2', '3'])})
    redis_client.hset('hash', 'age', 17)
    print(redis_client.hgetall('hash'))
    print(redis_client.hget('hash', 'name'))
    print(str(redis_client.hget('hash', 'ls')).split(','))
    # redis_client.expire('hash', 10) 只能对整体设置超时


def list_op():
    redis_client.lpush('queue', 1, 2, 3, 4)
    # print(redis_client.lrange('queue', 0, 10))
    # redis_client.lpush('queue', 1)
    # redis_client.lpush('queue', 2)
    # redis_client.lpush('queue', 3)
    print(redis_client.lpop('queue'))
    print(redis_client.lpop('queue'))
    print(redis_client.rpop('queue'))
    # redis_client.blpop('queue', timeout=10)  # 也可以定义超时操作


def set_op():
    redis_client.sadd('set', 1, 1, 3)
    print(redis_client.smembers('set'))
    print(redis_client.spop('set'))
    print(redis_client.smembers('set'))


def zset_op():
    redis_client.zadd('zset', {'a': 1, 'b': 2, 'c': 10, 'd': 20})
    print(redis_client.zcount('zset', 0, 10))
    redis_client.zincrby('zset', 1, 'a')
    print(redis_client.zscore('zset', 'a'))
    print(redis_client.zrank('zset', 'b'))
    print(redis_client.zrange('zset', 0, 2, desc=True, withscores=True))
    print(redis_client.zrangebyscore('zset', 0, 2, start=0, num=1, withscores=True))


def geo_op():
    redis_client.geoadd('geo', 114.780801, 28.387411, 'wife')
    redis_client.geoadd('geo', 114.780801, 28.387412, 'son')
    redis_client.geoadd('geo', 121.241101, 31.112532, 'husband')
    print(redis_client.geodist('geo', 'wife', 'husband', unit='km'))
    print(redis_client.georadiusbymember('geo', 'son', 10, unit='m', withdist=True, withcoord=True, withhash=True))
    print(redis_client.georadius('geo', 114.780801, 28.387411, 10, unit='m'))


def pub(_redis_client):
    while True:
        for i in range(3):
            msg = 'msg%d' % i
            _redis_client.publish('baobao', msg)
            print('publish msg:%s' % msg)
            time.sleep(4)


def sub(_redis_client, name):
    pubsub = _redis_client.pubsub()  # 先构建基础类
    pubsub.subscribe('baobao')  # 订阅频道
    for msg in pubsub.listen():
        print(msg)
        print('name :%s receive msg:%s' % (name, msg['data']))


def pubsub_op():
    threading.Thread(target=pub, args=(redis_client,)).start()
    threading.Thread(target=sub, args=(redis_client, 'a')).start()
    threading.Thread(target=sub, args=(redis_client, 'b')).start()


if __name__ == '__main__':
    redis_client = Redis.from_url('redis://:@localhost:6379/0')
    # kv()
    # hash_op()
    # list_op()
    # set_op()
    # zset_op()
    # geo_op()
    pubsub_op()
